package org.koron.ebs.util.field;

import java.util.HashMap;
import java.util.Map.Entry;

/**
 * 枚举元素，现在只接受值为字符串/整数/长整型
 * 
 * @author swan
 *
 */
public class EnumElement<T> {
	private HashMap<T, String> item = new HashMap<T, String>();
	boolean isBit = false;

	/**
	 * @param key
	 * @param value
	 * @return
	 * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
	 */
	public String put(T key, String value) {
		return item.put(key, value);
	}

	/**
	 * 获取指定值对应的描述,无分隔符
	 * 
	 * @param key 值
	 * @return 对应的描述
	 */
	public String getValue(T key) {
		return getValue(key, "");
	}

	/**
	 * 获取指定值对应的描述
	 * 
	 * @param key 值
	 * @param delimiter 分隔符
	 * @return 对应的描述
	 */
	public String getValue(T key, String delimiter) {
		if (isBit) {
			if (Integer.class.isAssignableFrom(key.getClass())) {
				StringBuilder sb = new StringBuilder();
				for (Entry<T, String> entry : item.entrySet()) {
					int key_int = ((Integer) entry.getKey()).intValue();
					if (((Integer) key & key_int) == key_int)
						sb.append(delimiter).append(entry.getValue());
				}
				return sb.substring(sb.length() > 0 ? delimiter.length() : 0);
			} else if (Long.class.isAssignableFrom(key.getClass())) {
				StringBuilder sb = new StringBuilder();
				for (Entry<T, String> entry : item.entrySet()) {
					int key_int = ((Long) entry.getKey()).intValue();
					if (((Long) key & key_int) == key_int)
						sb.append(delimiter).append(entry.getValue());
				}
				return sb.substring(sb.length() > 0 ? delimiter.length() : 0);
			}
		}
		return item.get(key);
	}

	/**
	 * @return 获取#{bare_field_comment}
	 */
	public HashMap<T, String> getItem() {
		return item;
	}

	/**
	 * @param 设置#{bare_field_comment}
	 */
	public void setItem(HashMap<T, String> item) {
		this.item = item;
	}

	/**
	 * @return 获取#{bare_field_comment}
	 */
	public boolean isBit() {
		return isBit;
	}

	/**
	 * @param 设置#{bare_field_comment}
	 */
	public void setBit(boolean isBit) {
		this.isBit = isBit;
	}
}